import { showLoadingToast, showSuccessToast, showFailToast, closeToast, showToast } from 'vant';
import { urlPath } from "@/api/index/urlPath.js";
import { routerPath } from "@/router/routerPath.js";
import ZYBW from "@/util/request/index.js";
import { ref, watch, provide, onMounted } from 'vue';
// import { useRouter, useRoute } from 'vue-router';
import { encryptAsDoNet, DecryptDoNet } from "@/util/desUtil.js";
import $ from "jquery";
class UiUtil {
    constructor() {
        this.urlPath = urlPath;
        this.routerPath = routerPath;
        this.httpObj = null;
        // this.useRoute = useRoute();
        // this.useRouter = useRouter();
        this.ref = ref
        this.watch = watch;
        this.onMounted = onMounted;
        this.user = JSON.parse(localStorage.getItem(urlPath.keys.user))
    }
    http = function () {
        if (null == this.httpObj) {
            this.httpObj = ZYBW;
        }
        return this.httpObj
    }
    toastSuccess = function (msg) {
        showSuccessToast(
            {
                message: msg,
                duration: 2000,
            }
        )
    }
    toastFail = function (msg) {
        showFailToast(
            {
                message: msg,
                duration: 2000,
            }
        )
    }
    toastLoading = function (msg) {
        if (null == msg || undefined == msg) {
            msg = '加载中...'
        }
        showLoadingToast(
            {
                message: msg,
                duration: 0, // 设置为 0 表示持续展示，不自动关闭
                forbidClick: true,// 禁止背景点击
            }
        )
    }
    toastLoadingMsg = function () {
        showLoadingToast(
            {
                message: '加载中...',
                duration: 0, // 设置为 0 表示持续展示，不自动关闭
                forbidClick: true,// 禁止背景点击
            }
        )
    }
    toastClose = function () {
        closeToast()
    }
    printPagePath = function (route) {
        console.log('----------------' + route.path + '-------------------------');
    }

    toast = function (msg) {
        showToast(msg)
    }
    // 加密
    encrypt = function (pwd) {
        return encryptAsDoNet(pwd)
    }
    // 解密
    decrypt = function (pwd) {
        return DecryptDoNet(pwd)
    }
    contains = function (obj, key) {
        var boo = obj.hasOwnProperty(key)
        return bool;
    }
    isNotNull = function (obj) {
        if (null == obj || undefined == obj) {
            return false;
        }
        if (Object.keys(obj).length == 0) return false
        return true;
    }
    isNull = function (obj) {
        if (null == obj || undefined == obj) {
            return true;
        }
        return false;
    }
    isArrNull = function (obj) {
        // debugger
        if (null == obj || undefined == obj) {
            return true;
        }
        if (!Array.isArray(obj)) {
            return true;  // 如果 obj 不是数组，返回 false
        }
        if (obj.length == 0) {
            return true
        }
        return false;
    }
    isObjNull = function (obj) {
        // debugger
        if (null == obj || undefined == obj) {
            return true;
        }
        if (Object.keys(obj).length == 0) {
            return true
        }
        return false;
    }
    isStrNull = function (str) {
        // debugger
        str = str + ''
        if (null == str || undefined == str || "undefined" == str) return true
        if (str.trim().length == 0) return true
        return false
    }
    arrLength = function (arr) {
        if (null == arr || undefined) return 0
        if (Array.isArray(arr)) arr.length
    }
    containsThreeCategories = function (str) {
        const hasDigit = /\d/.test(str); // 判断是否包含数字
        const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(str); // 判断是否包含特殊符号
        const hasUppercase = /[A-Z]/.test(str); // 判断是否包含大写字母
        const hasLowercase = /[a-z]/.test(str); // 判断是否包含小写字母

        // 统计满足条件的类别数
        const count = [hasDigit, hasSpecialChar, hasUppercase, hasLowercase].filter(Boolean).length;

        // 如果满足任意三种或更多，则返回 true
        return count >= 3;
    }

    /////////////////////////
    getNumberData2(value, dw, iskh) {
        try {
            if (this.isStrNull(value)) return "0";
            let d = parseFloat(value);
            if (d < 10) return "0";

            if (this.user.UserRoleID == '1') {
                d = d / 10000;
                dw = "万" + dw
            }
            if (iskh) {
                dw = ' (' + dw + ')';
            }
            let a = Math.round((d + Number.EPSILON) * 100) / 100;
            let roundedNum = a.toFixed(2)
            // console.log("四舍五入后的结果为：" + roundedNum);

            if (roundedNum > 0) {
                return roundedNum + dw;
            }

            let s = d.toString();
            if (s.includes(".")) {
                let s_length = s.split(".")[1];
                let sl = s_length.length;

                if (sl == 1) {
                    s = s + "0";
                    return s + dw;
                } else if (sl === 2) {
                    return s + dw;
                } else if (sl >= 3) {
                    let fmtstr = d.toFixed(3);
                    let nstr = fmtstr.charAt(fmtstr.length - 1);
                    let f = 0;

                    if (nstr != "0") {
                        f = parseFloat(fmtstr) + 0.01;
                    } else {
                        f = parseFloat(fmtstr);
                    }

                    let fmtst2 = f.toString();

                    if (fmtst2.split(".")[1].length === 3) {
                        fmtst2 = fmtst2.slice(0, -1);
                    } else if (fmtst2.split(".")[1].length > 3) {
                        fmtst2 = fmtst2.slice(0, fmtst2.indexOf(".") + 3);
                    }

                    return fmtst2 + dw;
                }
            }

        } catch (e) {
            console.error(e);
        }
        return value + dw;
    }
    $ = $
    vantHeader1 = function () {
        $('.van-nav-bar__title')
            .css('color', 'white')
            .css('font-size', '20px')
            .css('background', 'none')
        $('.van-nav-bar')
            .css('background', 'none')
        $('.van-icon')
            .css('color', 'white')
    }
    vantHeader2 = function () {
        $('.van-nav-bar__title')
            .css('color', 'white')
            .css('font-size', '20px')
            .css('background', 'none')
        $('.van-nav-bar')
            .css('background', 'none')
            .css('height', '60px')
        $('.van-icon')
            .css('color', 'white')
        $('.van-nav-bar').css('background', 'none')
        $('.van-nav-bar__content').css('background', 'none')

    }
    getRandomString =function(length) {
        const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        let result = '';
        const charactersLength = characters.length;
        for (let i = 0; i < length; i++) {
            result += characters.charAt(Math.floor(Math.random() * charactersLength));
        }
        return result;
    }
    getUser=function () {
        var user = JSON.parse(localStorage.getItem(urlPath.keys.user))
        return user;
    }
}
export default new UiUtil()
// 在初始化后再设置 http
// const uiUtilInstance = new UiUtil();
// import('@/util/request/index.js').then(module => {
//     if (!uiUtilInstance.isNotNull(uiUtilInstance.http)){
//         uiUtilInstance.http =module.default;
//     }
//
// });
//
// export default uiUtilInstance;
// import {onMounted, ref} from "vue";
// import { useRouter} from 'vue-router';
// const router = useRouter();
// uiUtil.printPagePath(router.currentRoute.value)
